import {
  Controller,
  Post,
  Body,
  UseGuards,
  Request,
  Get,
  Delete,
  Put,
  Param,
  Query,
} from '@nestjs/common';
import { ApiTags, ApiOperation } from '@nestjs/swagger';
import { CreatePageDto } from './dto/create-page.dto';
import { PageService } from './page.service';
import { Page } from './page.entity';
import { RemovePageDto } from './dto/remove-page.dto';
import { RetrievePageDto } from './dto/retrieve-page.dto';
import { UpdatePageDto } from './dto/update-page.dto';
import { FindPageDto } from './dto/find-page.dto';
import { JwtAuthGuardUser } from 'src/auth/guards/jwt-auth.guard';

@ApiTags('微页面')
@Controller('api/page')
export class PageController {
  constructor(private readonly pagesService: PageService) {}

  // 增加
  @UseGuards(JwtAuthGuardUser)
  @Post()
  @ApiOperation({ summary: '增加' })
  async create(@Body() createPageDto: CreatePageDto): Promise<Page> {
    return await this.pagesService.create(createPageDto);
  }
  // 复制
  @UseGuards(JwtAuthGuardUser)
  @Post("copy")
  @ApiOperation({ summary: '复制' })
  async copy(@Body() createPageDto: CreatePageDto): Promise<Page> {
    return await this.pagesService.copy(createPageDto);
  }
  // 删除
  @UseGuards(JwtAuthGuardUser)
  @Delete()
  @ApiOperation({ summary: '删除' })
  async remove(@Body() removePageDto: RemovePageDto): Promise<any> {
    return await this.pagesService.delete(removePageDto);
  }

  // 更新
  @UseGuards(JwtAuthGuardUser)
  @Put(':id')
  @ApiOperation({ summary: '更新' })
  async update(
    @Param() params: RetrievePageDto,
    @Body() updatePageDto: UpdatePageDto,
  ): Promise<any> {
    return await this.pagesService.update({
      id: params.id,
      updatePageDto,
    });
  }

  // 列表
  // @UseGuards(JwtAuthGuard)
  @Get()
  @ApiOperation({ summary: '列表' })
  async findAll(@Query() query: FindPageDto): Promise<Page> {
    return await this.pagesService.findAll(query);
  }

  // 根据 id 查找
  @Get(':id')
  @ApiOperation({ summary: '根据 id 查找' })
  async findOneById(@Param() params: RetrievePageDto): Promise<any> {
    return await this.pagesService.findOneById(params);
  }

  // 数量
  @Get('list/count')
  @ApiOperation({ summary: '数量' })
  async getCount() {
    return await this.pagesService.getCount();
  }
}
